/*  dreic -- a compiler for Drei
 *
 *  $Id: Position.scala 2970 2006-10-16 09:57:51Z mihaylov $
 */

package dreic


/** This class provides methods for encoding a location in
 *  a source file (line/column) into a single integer.
 */
object Position {

  /** number of bits reserved for the column.
   */
  val columnBits = 12

  val columnMask = (1 << columnBits) - 1

  /** undefined position
   */
  val UNDEFINED = 0

  /** first position in a source file
   */
  val FIRST = (1 << columnBits) | 1

  /** encode a line and column number into a single int
   */
  def encode(line: Int, col: Int): Int =
    (line << columnBits) | (col & columnMask)

  /** get the line number out of an encoded position
   */
  def line(pos: Int): Int =
    pos >>> columnBits

  /** return the column number of an encoded position
   */
  def column(pos: Int): Int =
    pos & columnMask

  def toString(pos: Int) = line(pos).toString() + ":" + column(pos)
}
